//
//  PLSandboxManager.m
//  PlatformCommon
//
//  Created by KimmanYip on 2017/9/26.
//
//

#import "PLSandboxManager.h"

@implementation PLSandboxManager

+ (NSString *)getTempDirectory {
    return NSTemporaryDirectory();
}

+ (NSString *)getDocumentsDirectory {
    NSArray *documentDirArr=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [documentDirArr firstObject];
}

+ (BOOL)fileExists:(NSString *)filePath {
    return [[NSFileManager defaultManager] fileExistsAtPath:filePath];
}

+ (BOOL)fileExistsInDocuments:(NSString *)fileName {
    NSString *filePath = [[self getDocumentsDirectory] stringByAppendingPathComponent:fileName];
    return [self fileExists:filePath];
}

+ (BOOL)createDirectoryInDocuments:(NSString *)dirName {
    NSString *path = [[self getDocumentsDirectory] stringByAppendingPathComponent:dirName];
    return [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}

+ (BOOL)deleteFileAtPath:(NSString *)filePath {
    return [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}

+ (BOOL)deleteFileInDocuments:(NSString *)fileName{
    NSString *filePath = [[self getDocumentsDirectory] stringByAppendingPathComponent:fileName];
    return [self deleteFileAtPath:filePath];
}

+ (BOOL)copyFileFrom:(NSString *)orgPath to:(NSString *)desPath {
    // 如果目的路径文件已经存在，则先删除
    if ([self fileExists:desPath]) {
        [self deleteFileAtPath:desPath];
    }
    return [[NSFileManager defaultManager] copyItemAtPath:orgPath toPath:desPath error:nil];
}

+ (BOOL)moveFileForm:(NSString *)orgPath to:(NSString *)desPath {
    return [[NSFileManager defaultManager] moveItemAtPath:orgPath toPath:desPath error:nil];
}

// 保存到沙盒
+ (BOOL)saveDataToSandbox:(NSData *)data fileName:(NSString *)fileName {
    NSString *path = [[self getDocumentsDirectory] stringByAppendingPathComponent:fileName];
    return [data writeToFile:path atomically:YES];
}

@end
